package com.ruoyi.crm.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.crm.domain.CustomerProduct;
import com.ruoyi.crm.mapper.ServiceContractMapper;
import com.ruoyi.crm.domain.ServiceContract;
import com.ruoyi.crm.service.IServiceContractService;

/**
 * 服务合同Service业务层处理
 *
 * @author ruoyi
 * @date 2021-08-30
 */
@Service
public class ServiceContractServiceImpl implements IServiceContractService
{
    @Autowired
    private ServiceContractMapper serviceContractMapper;

    /**
     * 查询服务合同
     *
     * @param id 服务合同主键
     * @return 服务合同
     */
    @Override
    public ServiceContract selectServiceContractById(String id)
    {
        return serviceContractMapper.selectServiceContractById(id);
    }

    /**
     * 查询服务合同列表
     *
     * @param serviceContract 服务合同
     * @return 服务合同
     */
    @Override
    public List<ServiceContract> selectServiceContractList(ServiceContract serviceContract)
    {
        return serviceContractMapper.selectServiceContractList(serviceContract);
    }

    /**
     * 新增服务合同
     *
     * @param serviceContract 服务合同
     * @return 结果
     */
    @Transactional
    @Override
    public int insertServiceContract(ServiceContract serviceContract)
    {
        int rows = serviceContractMapper.insertServiceContract(serviceContract);
        insertCustomerProduct(serviceContract);
        return rows;
    }

    /**
     * 修改服务合同
     *
     * @param serviceContract 服务合同
     * @return 结果
     */
    @Transactional
    @Override
    public int updateServiceContract(ServiceContract serviceContract)
    {
        serviceContract.setUpdateTime(DateUtils.getNowDate());
        serviceContractMapper.deleteCustomerProductByCustomerName(serviceContract.getId());
        insertCustomerProduct(serviceContract);
        return serviceContractMapper.updateServiceContract(serviceContract);
    }

    /**
     * 批量删除服务合同
     *
     * @param ids 需要删除的服务合同主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteServiceContractByIds(String[] ids)
    {
        return serviceContractMapper.deleteServiceContractByIds(ids);
    }

    /**
     * 删除服务合同信息
     *
     * @param id 服务合同主键
     * @return 结果
     */
    @Override
    public int deleteServiceContractById(String id)
    {
        serviceContractMapper.deleteCustomerProductByCustomerName(id);
        return serviceContractMapper.deleteServiceContractById(id);
    }

    @Override
    public ServiceContract getInfoByContractNo(String contractNo) {
        return serviceContractMapper.getInfoByContractNo(contractNo);
    }

    /**
     * 新增顾客产品信息信息
     *
     * @param serviceContract 服务合同对象
     */
    public void insertCustomerProduct(ServiceContract serviceContract)
    {
        List<CustomerProduct> customerProductList = serviceContract.getCustomerProductList();
        String id = serviceContract.getId();
        if (StringUtils.isNotNull(customerProductList))
        {
            List<CustomerProduct> list = new ArrayList<CustomerProduct>();
            for (CustomerProduct customerProduct : customerProductList)
            {
                customerProduct.setCustomerName(id);
                list.add(customerProduct);
            }
            if (list.size() > 0)
            {
                serviceContractMapper.batchCustomerProduct(list);
            }
        }
    }
}